import frappe
from frappe import _

@frappe.whitelist()
def hide_right_sidebar():
	"""
	隐藏右侧边栏
	"""
	try:
		frappe.local.response["hide_right_sidebar"] = True
		return {"success": True, "message": "右侧边栏已隐藏"}
	except Exception as e:
		frappe.log_error(f"隐藏右侧边栏失败: {str(e)}")
		return {"success": False, "error": str(e)}

@frappe.whitelist()
def show_right_sidebar():
	"""
	显示右侧边栏
	"""
	try:
		frappe.local.response["show_right_sidebar"] = True
		return {"success": True, "message": "右侧边栏已显示"}
	except Exception as e:
		frappe.log_error(f"显示右侧边栏失败: {str(e)}")
		return {"success": False, "error": str(e)}

@frappe.whitelist()
def toggle_right_sidebar():
	"""
	切换右侧边栏显示状态
	"""
	try:
		# 获取当前用户偏好设置
		user_preference = frappe.db.get_value("User Preference", {"user": frappe.session.user}, "hide_right_sidebar")
		
		if user_preference:
			# 如果当前是隐藏状态，则显示
			frappe.db.set_value("User Preference", {"user": frappe.session.user}, "hide_right_sidebar", 0)
			return {"success": True, "message": "右侧边栏已显示", "hidden": False}
		else:
			# 如果当前是显示状态，则隐藏
			frappe.db.set_value("User Preference", {"user": frappe.session.user}, "hide_right_sidebar", 1)
			return {"success": True, "message": "右侧边栏已隐藏", "hidden": True}
			
	except Exception as e:
		frappe.log_error(f"切换右侧边栏状态失败: {str(e)}")
		return {"success": False, "error": str(e)}

@frappe.whitelist()
def get_sidebar_state():
	"""
	获取当前用户的右侧边栏状态
	"""
	try:
		user_preference = frappe.db.get_value("User Preference", {"user": frappe.session.user}, "hide_right_sidebar")
		return {
			"success": True, 
			"hidden": bool(user_preference),
			"message": "获取侧边栏状态成功"
		}
	except Exception as e:
		frappe.log_error(f"获取侧边栏状态失败: {str(e)}")
		return {"success": False, "error": str(e)}